import store from "../store";
import util from "./util";
import cache from "@/utils/cache";
import {
	wxAuthorize
} from "./wechat";
// 检查token 是否过期
export function checkTokenEndTime() {
	const endTimeStr = uni.getStorageSync(`tokenStr_${process.env.ENV_TYPE}_3H5`);
	const timeStr = new Date().getTime();
	return timeStr > endTimeStr; // 如果现在的时间戳大于本地存进去的时间戳  则过期
}

let executedLogout = false;
export function authTokenClear() {
	if (!executedLogout) {
		executedLogout = true;
		store.commit("LOGOUT");
		util.Tips({
			title: "请重新登录",
		});
		setTimeout(() => {
			uni.navigateTo({
				url: "/pagesWater/users/login",
			});
			executedLogout = false
		}, 1000);
	}
}
// export function authTokenClear() {
//   store.commit("LOGOUT");

//   util.Tips({
//     title: "请重新登录",
//   });
//   setTimeout(() => {
//     uni.reLaunch({
//       url: "/pagesWater/users/login",
//     });
//   }, 1500);
// }

export function saveLoginEndTime(expiresIn) {
	// #ifdef H5  || MP-WEIXIN
	const str = new Date(expiresIn.replaceAll('-', '/')).getTime();
	// #endif
	// #ifdef APP-PLUS
	const str = new Date(expiresIn).getTime();
	// #endif

	store.commit("SRTTOKENSTR", str);
}

export function loginSucessBack() {
	let par = cache.get('params') && JSON.parse(cache.get('params'))
	if(par?.spreadUid) {
		delete par.spreadUid
		cache.set("params", par);
	}
	let pages = getCurrentPages();
	let pageArr = Array.from(new Set(pages.map((item) => item.route)));

	if (pageArr.length > 1) {
		setTimeout(() => {
			uni.navigateBack({
				delta: 1,
			});
		}, 500);
	} else {
		uni.switchTab({
			url: "/pages/index/index",
		});
	}

	console.log(pageArr);
}



export function deleteSpuid() {
	let par = cache.get('params') && JSON.parse(cache.get('params'))
	if(par?.spreadUid) {
		delete par.spreadUid
		cache.set("params", par);
	}
}
export function authParams(data) {
  // 过滤无效值（null/undefined/空字符串）
  const filtered = Object.entries(data).reduce((acc, [key, value]) => {
    if (value !== "undefined" && value !== undefined ) {
      acc[key] = value;
    }
    return acc;
  }, {});
  
  return filtered;
}